Skip to content

Conversation

@jurahul
Copy link
Contributor

@jurahul jurahul commented Nov 18, 2025

Add a RAII IfGuardEmitter to insert simple #if guards and adopt it in InstrInfoEmitter.

Add a RAII `IfGuardEmitter` to insert simple #if guards and adopt
it in InstrInfoEmitter.
@github-actions
Copy link

github-actions bot commented Nov 18, 2025

🐧 Linux x64 Test Results

  • 186341 tests passed
  • 4859 tests skipped

@jurahul jurahul marked this pull request as ready for review November 18, 2025 23:19
@jurahul jurahul requested a review from jayfoad November 18, 2025 23:19
@llvmbot
Copy link
Member

llvmbot commented Nov 18, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes

Add a RAII IfGuardEmitter to insert simple #if guards and adopt it in InstrInfoEmitter.


Full diff: https://github.com/llvm/llvm-project/pull/168616.diff

2 Files Affected:

  • (modified) llvm/include/llvm/TableGen/CodeGenHelpers.h (+18)
  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+17-18)
diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h
index 1b1b5e63a8fc4..f7e732a4b232d 100644
--- a/llvm/include/llvm/TableGen/CodeGenHelpers.h
+++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h
@@ -46,6 +46,24 @@ class IfDefEmitter {
   bool LateUndef;
 };
 
+// Simple RAII helper for emitting #if guard. It emits:
+// <IfGuard> <Condition>
+// #endif // <Condition>
+class IfGuardEmitter {
+public:
+  IfGuardEmitter(raw_ostream &OS, StringRef Condition,
+                 StringRef IfGuard = "#if")
+      : Condition(Condition.str()), OS(OS) {
+    OS << IfGuard << " " << Condition << "\n\n";
+  }
+
+  ~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; }
+
+private:
+  std::string Condition;
+  raw_ostream &OS;
+};
+
 // Simple RAII helper for emitting header include guard (ifndef-define-endif).
 class IncludeGuardEmitter {
 public:
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 32994c12aa98b..0d13a6f173934 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -911,24 +911,23 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
     }
   }
 
-  OS << "#if defined(GET_INSTRINFO_MC_DESC) || "
-        "defined(GET_INSTRINFO_CTOR_DTOR)\n";
-
-  OS << "namespace llvm {\n\n";
-
-  OS << "struct " << TargetName << "InstrTable {\n";
-  OS << "  MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
-  OS << "  static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
-        "\"Unwanted padding between Insts and OperandInfo\");\n";
-  OS << "  MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
-  OS << "  static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
-        "\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
-  OS << "  MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n";
-  OS << "};\n\n";
-
-  OS << "} // end namespace llvm\n";
-  OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || "
-        "defined(GET_INSTRINFO_CTOR_DTOR)\n\n";
+  {
+    IfGuardEmitter IfGuard(
+        OS,
+        "defined(GET_INSTRINFO_MC_DESC) || defined(GET_INSTRINFO_CTOR_DTOR)");
+    NamespaceEmitter NS(OS, "llvm");
+
+    OS << "struct " << TargetName << "InstrTable {\n";
+    OS << "  MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
+    OS << "  static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
+          "\"Unwanted padding between Insts and OperandInfo\");\n";
+    OS << "  MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
+    OS << "  static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
+          "\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
+    OS << "  MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U)
+       << "];\n";
+    OS << "};";
+  }
 
   const CodeGenRegBank &RegBank = Target.getRegBank();
   const CodeGenHwModes &CGH = Target.getHwModes();

@jurahul jurahul requested a review from s-barannikov November 18, 2025 23:30
@jurahul jurahul force-pushed the nfc_add_ifguard_emitter branch from 77a5078 to ae51f61 Compare November 18, 2025 23:44
Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jurahul jurahul merged commit 139f726 into llvm:main Nov 19, 2025
10 checks passed
@jurahul jurahul deleted the nfc_add_ifguard_emitter branch November 19, 2025 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants